﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomerService
{
    public partial class CustomerTab : System.Web.UI.UserControl
    {
        private SalesManagerEntities _sme;
        //string[] canadianStates = new string[] { "AB", "BC", "MB", "NB", "NF", "NS", "NU", "ON", "PE", "QC", "SK", "YT" };
        protected bool _enabled = true;
        protected tblCustomer _customer;

        protected void Page_Load(object sender, EventArgs e)
        {
            _sme = (SalesManagerEntities)Session["sme"];
            SetEnabled();

            if (IsPostBack)
            {
                if (((_customer.Person.FirstName == null) ? string.Empty : _customer.Person.FirstName.Trim()) != txtFirstName.Text.Trim() ||
                    ((_customer.Person.LastName == null) ? string.Empty : _customer.Person.LastName.Trim()) != txtLastName.Text.Trim())
                    _customer.Person.Name = txtFirstName.Text.Trim() + " " + txtLastName.Text.Trim();

                _customer.Person.FirstName = txtFirstName.Text;
                _customer.Person.LastName = txtLastName.Text;
                if ((_customer.Person.Address.Address1 ?? string.Empty).Trim() != txtAddress1.Text.Trim())
                    _customer.Person.Address.Address1 = txtAddress1.Text;
                if ((_customer.Person.Address.Address2 ?? string.Empty).Trim() != txtAddress2.Text.Trim())
                    _customer.Person.Address.Address2 = txtAddress2.Text;
                if ((_customer.Person.Address.Address3 ?? string.Empty).Trim() != txtInternationalAddress2.Text.Trim())
                    _customer.Person.Address.Address2 = txtInternationalAddress2.Text;
                if ((_customer.Person.Address.Address3 ?? string.Empty).Trim() != txtInternationalAddress3.Text.Trim())
                    _customer.Person.Address.Address3 = txtInternationalAddress3.Text;
                if ((_customer.Person.Address.City ?? string.Empty).Trim() != txtCity.Text.Trim())
                    _customer.Person.Address.City = txtCity.Text;
                if ((_customer.Person.Address.PostalCode ?? string.Empty).Trim() != txtZip.Text.Trim())
                    _customer.Person.Address.PostalCode = txtZip.Text;
                if ((_customer.Person.Address.State ?? string.Empty).Trim() != ddState.SelectedValue)
                    _customer.Person.Address.State = ddState.SelectedValue;
                _customer.Person.Address.Country = _sme.codeCountry.Find(new object[] { int.Parse(ddCountry.SelectedValue) });
                _customer.Person.Language = _sme.codeLanguage.Find(new object[] { int.Parse(ddLanguage.SelectedValue) });

                _customer.AllowEmail = chkAllowEmail.Checked;
                _customer.AllowMail = chkAllowMail.Checked;
                _customer.AllowPhone = chkAllowPhone.Checked;
                _customer.AllowRent = chkAllowRent.Checked;
                _customer.DoNotProcess = chkDoNotProcess.Checked;
            }
        }

        private void SetEnabled()
        {
            bool USAddress = (ddCountry.SelectedValue == "1" || ddCountry.SelectedValue == "40");

            if (USAddress)
            {
                RequiredCity.Enabled = true;
                RequiredZip.Enabled = true;
                RequiredIntlAddress2.Enabled = false;
            }
            else
            {
                RequiredCity.Enabled = false;
                RequiredZip.Enabled = false;
                RequiredIntlAddress2.Enabled = true;
            }
        }

        public new void Load(SalesManagerEntities context, tblCustomer customer)
        {
            _customer = customer;

            if (!IsPostBack)
            {
                // Populate the Language drop down.
                if (ddLanguage.Items.Count == 0)
                    foreach (codeLanguage l in from c in context.codeLanguage.Local orderby c.Name select c)
                        ddLanguage.Items.Add(new ListItem(l.Name, l.LanguageID.ToString()));
                // Populate the State drop down.
                if (ddState.Items.Count == 0)
                    foreach (codeState l in from c in context.codeState.Local orderby c.Name select c)
                        ddState.Items.Add(new ListItem(l.Name, l.Abbrev));
                // Populate the country drop down.
                if (ddCountry.Items.Count == 0)
                    foreach (codeCountry l in from c in context.codeCountry.Local orderby c.Name select c)
                        ddCountry.Items.Add(new ListItem(l.Name, l.CountryID.ToString()));

                txtName.Text = customer.Person.Name;
                txtFirstName.Text = customer.Person.FirstName;
                txtLastName.Text = customer.Person.LastName;
                txtAddress1.Text = customer.Person.Address.Address1;
                txtAddress2.Text = customer.Person.Address.Address2;
                txtInternationalAddress2.Text = customer.Person.Address.Address2;
                txtInternationalAddress3.Text = customer.Person.Address.Address3;
                txtCity.Text = customer.Person.Address.City;
                txtZip.Text = customer.Person.Address.PostalCode;
                if (customer.Person.Address.State != null)
                    ddState.SelectedValue = customer.Person.Address.State;
                ddCountry.SelectedValue = customer.Person.Address.Country.CountryID.ToString();
                ddLanguage.SelectedValue = customer.Person.Language.LanguageID.ToString();

                chkAllowEmail.Checked = customer.AllowEmail;
                chkAllowMail.Checked = customer.AllowMail;
                chkAllowPhone.Checked = customer.AllowPhone;
                chkAllowRent.Checked = customer.AllowRent;
                chkDoNotProcess.Checked = customer.DoNotProcess;
            }

            ContactPointList.Load(context, customer);
        }

    }
}